package com.eqdd.yiqidian.ui.share;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.GroupListResultBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.subscribers.DisposableSubscriber;
import io.rong.imlib.model.Conversation;

public class ShareQunzuListActivity extends BaseActivity {
    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx<SelectBean> slimAdapterEx;
    private ArrayList<SelectBean> datas;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "群组");
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_04, new SlimInjector<SelectBean>() {
            @Override
            public void onInject(SelectBean data, IViewInjector injector) {
                injector.text(R.id.tv_content, data.getContent())
                        .imageCircle(R.id.iv_head, data.getHeadImg())
                        .checked(R.id.checkbox, data.isChecked())
                        .invisible(R.id.checkbox);
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Bundle bundle = new Bundle();
                    bundle.putString("groupId", slimAdapterEx.getDataItem(position).getGuid());
                    startActivityForResult(ShareUserActivity.class, bundle, Config.SHARE_USER);
                });
    }

    private void refresh() {
        datas = new ArrayList<>();
        OkGo.<HttpResult<List<GroupListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.GROUP_LIST_INFO)
                .params("userGuid", GreenDaoUtil.getUser().getGuid())
                .execute(new JsonCallBack<HttpResult<List<GroupListResultBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<GroupListResultBean>>> response) {
                        Flowable.fromIterable(response.body().getItems())
                                .subscribe(dispo);
                    }
                });
    }


    DisposableSubscriber dispo = new DisposableSubscriber<GroupListResultBean>() {
        @Override
        public void onNext(GroupListResultBean itemBean) {

            SelectBean bean = new SelectBean();
            bean.setGuid(itemBean.getGroupid());
            bean.setHeadImg(itemBean.getGroupphoto());
            bean.setContent(itemBean.getGroupname());
            bean.setType(Conversation.ConversationType.PRIVATE);
            datas.add(bean);
        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onComplete() {

            slimAdapterEx.updateData(datas);
        }
    };

    @Override
    public void setView() {
        refresh();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onDestroy() {
        if (dispo != null && !dispo.isDisposed()) {
            dispo.dispose();
        }
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            setResult(Config.SUCCESS, data);
            finish();
        }
    }
}
